/* This file is part of FireEscape.
 *
 * FireEscape is the intellectual work of Phillip Cheng & Allen Park.
 * All future releases or modifications of this program must include this
 * attribuation as according to the Creative Commons BY-SA license.
 *
 * FireEscape is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * FireEscape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FireEscape.  If not, see <http://www.gnu.org/licenses/>.
 *
 * All modifications to this program should include this original statement.
 *
 * If you modify, distribute, or use the program , please contact
 * fireescape@kloudvine.com
 * http://code.google.com/p/fireescape/
 *
 * If you are interested in using FireEscape for your structure, please contact us
 * for support.
 *
 * If you do use FireEscape, please consider donating to the project.
 */
package fireescape;

import java.awt.Component;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author chengP
 */
public class Handler extends Thread {

    private Socket socket;
    private boolean run = true;

    /**
     *
     * @param s
     */
    public Handler(Socket s) {
        super("Handler");
        socket = s;
    }

    /**
     *
     */
    @Override
    public void run() {

            Logger.getLogger("FireEscapeLog").log(Level.INFO, "Server processing client at:" + socket.getInetAddress());
            try {
                //System.out.println("in try");
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                //System.out.println("past in");
                //System.out.println(socket.getInetAddress());
                Packet input = (Packet) in.readObject();
                //System.out.println(input.getString());
                if (input.getString().equalsIgnoreCase("returnGenome")) {
                    //System.out.println("replacing genome");
                    Logger.getLogger("FireEscapeLog").log(Level.INFO, "Score +1: " + input.senderID());
                    ServerAI.replaceGenome(input.getGenome());
                    System.out.println("Time: " + input.getGenome().getTime());
                    input = null;

                } else if (input.getString().equalsIgnoreCase("getGenome")) {
                    //System.out.println("Getting genome");

                    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                    Packet p = ServerAI.getPacket();

                    out.writeObject(p);
                    //Route to BMP
                    RouteToImage g = new RouteToImage(p.getGenome());
                    Component applet = g;
                    g.init();

                    try {
                        // Set System L&F
                        UIManager.setLookAndFeel(
                                UIManager.getSystemLookAndFeelClassName());
                    } catch (UnsupportedLookAndFeelException e) {
                        //not important
                    } catch (ClassNotFoundException e) {
                        //not important
                    } catch (InstantiationException e) {
                        //not important
                    } catch (IllegalAccessException e) {
                        //not important
                    }
                    JFrame frame = new JFrame("RouteToImage");

                    frame.getContentPane().add(applet);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setSize(FireEscapeGUI.xSize + 150, FireEscapeGUI.ySize + 5);
                    g.printRoute();
                    Logger.getLogger("FireEscapeLog").log(Level.INFO, "RouteBMP created: " + p.getGenome().getHash());
                    out.flush();
                    out.close();
                    frame.dispose();
                    g.destroy();
                    frame = null;
                    out = null;
                    g = null;
                    applet = null;
                    frame = null;
                    p = null;
                    input = null;
                } else {
                    Logger.getLogger("FireEscapeLog").log(Level.SEVERE, "Client returned string: +" + input.getString());
                }
                in.close();
                socket.close();
                in = null;
                socket = null;
                input = null;
                return;


            } catch (IOException e) {
                System.err.println(e);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger("FireEscapeLog").log(Level.SEVERE, "Something strange has happened");
                Logger.getLogger("FireEscapeLog").log(Level.SEVERE, null, ex);
            } finally {
            }
        }
    }

